package com.yxplearn.mybatisplus.deluxe;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class InsertAll extends AbstractMethod {


    /**
     * @param methodName 方法名
     * @since 3.5.0
     */
    protected InsertAll(String methodName) {
        super(methodName);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("<script>\n");
//        stringBuilder.append("<foreach collection=\"coll\" item=\"item\" index=\"index\" suffixOverrides=\";\" open=\"(\" separator=\");(\" close=\")\">\n");
        stringBuilder.append("<foreach collection=\"coll\" item=\"item\" index=\"index\" separator=\";\" suffixOverrides=\";\" >\n");
        stringBuilder.append(prepareFieldSql(tableInfo));
        stringBuilder.append("VALUES");
        stringBuilder.append(prepareValuesSqlForMysqlBatch(tableInfo));
        stringBuilder.append("</foreach>\n</script>");

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, stringBuilder.toString(), modelClass);
        return this.addInsertMappedStatement(mapperClass, modelClass, "insertAll", sqlSource, new NoKeyGenerator(), null, null);
    }


    private String prepareFieldSql(TableInfo tableInfo) {
        StringBuilder fieldSql = new StringBuilder();
        fieldSql.append("INSERT INTO ").append(tableInfo.getTableName()).append("\n");
        fieldSql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
        fieldSql.append(tableInfo.getKeyColumn()).append(",\n");
        tableInfo.getFieldList().forEach(x -> {
            fieldSql.append("<if test=\"item.").append(x.getProperty()).append(" != null\">");
            fieldSql.append(x.getColumn()).append(",");
            fieldSql.append("</if>\n");
        });
        fieldSql.append("</trim>");
        return fieldSql.toString();
    }

    private String prepareValuesSqlForMysqlBatch(TableInfo tableInfo) {
        final StringBuilder valueSql = new StringBuilder();
        valueSql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
        valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},\n");
        tableInfo.getFieldList().forEach(x -> {
            valueSql.append("<if test=\"item.").append(x.getProperty()).append(" != null\">");
            valueSql.append("#{item.").append(x.getProperty());
            if (x.getTypeHandler() != null) {
                valueSql.append(",typeHandler=").append(x.getTypeHandler().getName());
            }
            valueSql.append("},");
            valueSql.append("</if>\n");
        });
        valueSql.append("\n</trim>");
        return valueSql.toString();
    }

//    @Override
//    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
//        final String sql = "<script>\ninsert into %s %s values %s\n</script>";
//        String columnScript = SqlScriptUtils.convertTrim(tableInfo.getAllInsertSqlColumnMaybeIf(),
//                LEFT_BRACKET, RIGHT_BRACKET, null, COMMA);
//        String valueSql = SqlScriptUtils.convertForeach(tableInfo.getAllInsertSqlPropertyMaybeIf("ite1m."), Constants.COLLECTION, null,"item", COMMA);
//        final String sqlResult = String.format(sql, tableInfo.getTableName(), columnScript, valueSql);
//        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
//        return this.addInsertMappedStatement(mapperClass, modelClass, "insertAll", sqlSource, new NoKeyGenerator(), null, null);
//    }
//
//    private String prepareFieldSql(TableInfo tableInfo) {
//        StringBuilder fieldSql = new StringBuilder();
//        fieldSql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
//        fieldSql.append(tableInfo.getKeyColumn()).append(",\n");
//        tableInfo.getFieldList().forEach(x -> {
//            fieldSql.append("<if test=\"").append(x.getProperty()).append(" != null\">");
//            fieldSql.append(x.getColumn());
//            fieldSql.append(",");
//            fieldSql.append("</if>\n");
//        });
//        fieldSql.append("</trim>");
//        return fieldSql.toString();
//    }
//
//    private String prepareValuesSqlForMysqlBatch(TableInfo tableInfo) {
//        final StringBuilder valueSql = new StringBuilder();
//        valueSql.append("<foreach collection=\"coll\" item=\"item\" index=\"index\" suffixOverrides=\",\" open=\"(\" separator=\"),(\" close=\")\">\n");
//        valueSql.append("#{item.").append(tableInfo.getKeyProperty()).append("},\n");
//        tableInfo.getFieldList().forEach(x -> {
//            valueSql.append("<if test=\"item.").append(x.getProperty()).append(" != null\">");
//            valueSql.append("#{item.").append(x.getProperty());
//            if (x.getTypeHandler() != null) {
//                valueSql.append(",typeHandler=").append(x.getTypeHandler().getName());
//            }
//            valueSql.append("},");
//            valueSql.append("</if>\n");
//        });
//        valueSql.append("</foreach>");
//        return valueSql.toString();
//    }
}
